home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / archival / mirror-2.1 / dateconv.pl < prev    next >
Encoding:
Perl Script  |  1993-06-28  |  2.6 KB  |  104 lines

  1. # Convert a date into a time.
  2. # By Lee McLoughlin <lmjm@doc.ic.ac.uk>
  3. #  You can do what you like with this except claim that you wrote it or
  4. #  give copies with changes not approved by Lee.  Neither Lee nor any other
  5. #  organisation can be held liable for any problems caused by the use or
  6. #  storage of this package.
  7. # $Id: dateconv.pl,v 2.1 1993/06/28 15:04:22 lmjm Exp lmjm $
  8. # $Log: dateconv.pl,v $
  9. # Revision 2.1  1993/06/28  15:04:22  lmjm
  10. # Full 2.1 release
  11. #
  12.  
  13. # input date and time string from ftp "ls -l" format ("Feb 01 13:25"),
  14. # return data and time string in Unix format "dd Mmm YY HH:MM", "such as
  15. # "1 Feb 92 13:25"
  16. sub lstime_to_standard
  17. {
  18.     local( $ls ) = @_;
  19.  
  20.     return &time_to_standard( &lstime_to_time( $ls ) );
  21. }
  22.  
  23. package dateconv;
  24.  
  25. require 'timelocal.pl';
  26.  
  27. @months = ( "zero", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" );
  28.  
  29. $month_num{ "jan" } = 0;
  30. $month_num{ "feb" } = 1;
  31. $month_num{ "mar" } = 2;
  32. $month_num{ "apr" } = 3;
  33. $month_num{ "may" } = 4;
  34. $month_num{ "jun" } = 5;
  35. $month_num{ "jul" } = 6;
  36. $month_num{ "aug" } = 7;
  37. $month_num{ "sep" } = 8;
  38. $month_num{ "oct" } = 9;
  39. $month_num{ "nov" } = 10;
  40. $month_num{ "dec" } = 11;
  41.  
  42. ( $mn, $yr ) = (localtime)[ 4, 5 ];
  43.  
  44.  
  45. # input date and time string from ftp "ls -l", such as Mmm dd yyyy or
  46. # Mmm dd HH:MM,
  47. # return $time number via gmlocal( $string ).
  48. sub main'lstime_to_time
  49. {
  50.     package dateconv;
  51.  
  52.     local( $date ) = @_;
  53.  
  54.     local( $mon, $day, $hours, $mins, $month, $year );
  55.  
  56.     # Unix ls, dls and Netware
  57.     if( $date =~ /^(\w\w\w)\s+(\d+)\s+((\d\d\d\d)|((\d+):(\d+)))$/ ){
  58.         ($mon, $day, $year, $hours, $mins) = ($1, $2, $4, $6, $7);
  59.     }
  60.     elsif( $date =~ /^(\d+)\s+(\w\w\w)\s+((\d\d\d\d)|((\d+):(\d+)))$/ ){
  61.         ($day, $mon, $year, $hours, $mins) = ($1, $2, $4, $6, $7);
  62.     }
  63.     # VMS style
  64.     elsif( $date =~ /(\d+)-(\S+)-(\d+)\s+(\d+):(\d+)/ ){
  65.         ($day, $mon, $year, $hours, $mins) = ($1, $2, $3, $4, $5);
  66.     }
  67.     else {
  68.         printf STDERR "invalid date $date\n";
  69.         return time;
  70.     }
  71.     
  72.     $mon =~ tr/A-Z/a-z/;
  73.     $month = $month_num{ $mon };
  74.  
  75.     if( $year !~ /\d\d\d\d/ ){
  76.         $year = $yr;
  77.         $year-- if( $month > $mn );
  78.     }
  79.     if( $year > 1900 ){
  80.         $year -= 1900;
  81.     }
  82.      
  83.     if( $use_timelocal ){
  84.         return &timelocal( 0, $mins, $hours, $day, $month, $year );
  85.     }
  86.     else {
  87.         return &timegm( 0, $mins, $hours, $day, $month, $year );
  88.     }
  89. }
  90.  
  91. # input time number, output GMT string as "dd Mmm YY HH:SS"
  92. sub main'time_to_standard
  93. {
  94.     package dateconv;
  95.  
  96.     local( $time ) = @_;
  97.  
  98.     local( $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst ) =
  99.          gmtime( $time );
  100.     return sprintf( "%2d $months[ $mon + 1 ] %2d %02d:%02d", $mday, $year, $hour, $min );
  101. }
  102.  
  103. 1;
  104.